package cn.tedu.v1_1;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;


public class Client {

    private Socket socket;



    public Client() {
        try {
            socket = new Socket("127.0.0.1", 6666);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void start() {

        OutputStream os = null;
        InputStream is=null;

        OutputStreamWriter osw = null;
        InputStreamReader isr=null;

        BufferedWriter bw = null;
        BufferedReader br=null;
        PrintWriter pw = null;

        try {
            os = socket.getOutputStream();

            is=socket.getInputStream();
            osw = new OutputStreamWriter(os, "UTF8");

            isr=new InputStreamReader(is,"UTF8");

            bw = new BufferedWriter(osw);

            br=new BufferedReader(isr);

            pw = new PrintWriter(bw, true);

                     /* String str=null;

                             while(true){
                           str=br.readLine();
                         System.out.println("服务器端发来文本："+str);
                      }*/


            System.out.println("对服务器端说话：");
            Scanner input = new Scanner(System.in);
            String line = null;
            while (true) {
                line = input.nextLine();
                pw.println(line);
                System.out.println("写出成功~");
            }

        } catch(FileNotFoundException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if (pw!=null)
                pw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


        public static void main (String[]args){

            Client client = new Client();
            client.start();

        }
    }
